package com.fanly.weixin.utils;

import cn.hutool.core.util.StrUtil;
import com.fanly.weixin.entity.districtId.City;
import com.fanly.weixin.entity.districtId.District;
import com.fanly.weixin.entity.districtId.DistrictId;
import com.fanly.weixin.entity.districtId.Result;
import com.fanly.weixin.entity.postcode.PostCode;
import com.google.gson.Gson;

/**
 * 邮政编码工具类
 * @author fanlizhi
 *
 */
public class PostCodeUtils {

	/**
	 * 通过邮政编码获取地区
	 * @param postCode
	 * @return
	 */
	public static String getPostCode(String postCode){
		String httpUrl = "http://apis.haoservice.com/lifeservice/postcode/query";
		String httpArg = "postcode="+postCode+"&page=1&pagesize=20&key=769ac334487d4d9182b917baa41e4299";   // http://apis.haoservice.com/lifeservice/postcode/query?postcode=215001&page=1&pagesize=20&key=您申请的APPKEY
		System.out.println(httpUrl+httpArg);
		String jsonResult  = UrlUtils.getData(httpUrl, httpArg);
		if(StrUtil.isEmpty(jsonResult)){
			return "";
		}else{
			//转换成对应的bean
			Gson gson = new Gson();
			PostCode postCodeDto = gson.fromJson(jsonResult, PostCode.class);
			System.out.println(postCodeDto);
			StringBuffer buffer = new StringBuffer("邮政编码"+postCode+"所对应的地区是：  ");
			if( postCodeDto!=null && 0==postCodeDto.getError_code() 
					&& "成功".equals(postCodeDto.getReason()) && postCodeDto.getResult().getList()!=null && postCodeDto.getResult().getList().size()>0){
				buffer.append(postCodeDto.getResult().getList().get(0).getProvince()+postCodeDto.getResult().getList().get(0).getCity()+postCodeDto.getResult().getList().get(0).getDistrict());
				return buffer.toString();
			}else{
				return "";
			}
		}
	}
	/**
	 * 通过地区获取邮政编码
	 * @param district
	 * @return
	 */
	public static String getPostCodeByDistrict(String district){
		String[] districts = district.split(",");
		if(districts==null || districts.length<=0){
			return "";
		}
		String httpUrl = "http://apis.haoservice.com/lifeservice/postcode/pcd";
		String httpArg = "key=769ac334487d4d9182b917baa41e4299";  
		System.out.println(httpUrl+"?"+httpArg);
		String jsonResult  = UrlUtils.getData(httpUrl, httpArg);
		if(StrUtil.isEmpty(jsonResult)){
			return "";
		}else{
			//转换成对应的bean
			Gson gson = new Gson();
			DistrictId districtId = gson.fromJson(jsonResult, DistrictId.class);
			System.out.println(districtId);
			String pid = StrUtil.EMPTY;
			String cid = StrUtil.EMPTY;
			String did = StrUtil.EMPTY;
			StringBuffer buffer = new StringBuffer(district+"所对应的邮政编码是：  ");
			if( districtId!=null && 0==districtId.getError_code() && "Success".equals(districtId.getReason()) ){
				if(districtId.getResult()!=null){
					for(Result result :districtId.getResult()){
						if(districts[0].equals(result.getPrivince())){
							pid = result.getId();
							for(City city :result.getCity()){
								if(districts[1].equals(city.getCity())){
									cid = city.getId();
									for(District districtDto :city.getDistrict()){
										if(districts[2].equals(districtDto.getDistrict())){
											did = districtDto.getId();
										}
									}
								}
							}
						}
					}
				}
				String jsonResult1  = UrlUtils.getData("http://apis.haoservice.com/lifeservice/postcode/search",
														"key=769ac334487d4d9182b917baa41e4299&pid="+pid+"&cid="+cid+"&did="+did+"&paybyvas=false");
				PostCode postCodeDto = gson.fromJson(jsonResult1, PostCode.class);
				if( postCodeDto!=null && 0==postCodeDto.getError_code() 
						&& "成功".equals(postCodeDto.getReason()) && postCodeDto.getResult().getList()!=null && postCodeDto.getResult().getList().size()>0){
					buffer.append(postCodeDto.getResult().getList().get(0).getPostNumber());
					String str = buffer.toString();
					str = str.substring(0, str.length()-2)+"00";
					return str;
				}else{
					return "";
				}
			}else{
				return "";
			}
		}
	}
	public static void main(String[] args) {
//		System.out.println("-----"+PostCodeUtils.getPostCode("233300"));
		System.out.println("111199".substring(0, "111199".length()-2));
	}
}
